// tongji.js
const tongjiDao = require('../dao/tongji');
const util = require('../utilitys');
/**
 * 添加统计接口
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.getTongji = async (ctx) => {
  try {
    let result = await tongjiDao.tongji(ctx);
    if (result.length > 0) {
      ctx.body = util.resultSuccessJson(undefined, undefined, result);
    } else {
      ctx.body = util.resultSuccessJson(undefined, '账号或密码错误', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
};
/**
 * 查询统计接口
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.gettongjiList = async (ctx) => {
  try {
    let result = await tongjiDao.gettongjiList(ctx);
    if (result.length > 0) {
      ctx.body = util.resultSuccessJson(undefined, undefined, {
        list: result,
        indexTotal: result.filter((e) => {
          return e.type === 1;
        }).length,
        downTotal: result.filter((e) => {
          return e.type === 2;
        }).length,
        searchTotal: result.filter((e) => {
          return e.type === 3;
        }).length,
      });
    } else {
      ctx.body = util.resultSuccessJson(undefined, '错误', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
};
/**
 * 查询统计接口
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.gettongjiTypeList = async (ctx) => {
  try {
    // let result = await tongjiDao.admintongji(ctx, { phone, psd });
    let result = await tongjiDao.gettongjiList(ctx);
    if (result.length > 0) {
      ctx.body = util.resultSuccessJson(undefined, undefined, {
        list: result,
        total: result.length,
      });
    } else {
      ctx.body = util.resultSuccessJson(undefined, '错误', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
};
/**
 * 查询统计接口
 * @param ctx
 * @returns {Promise<boolean>}
 * @constructor
 */
exports.tongjiTotal = async (ctx) => {
  try {
    let result = await tongjiDao.tongjiTotal(ctx);
    let total = 0;
    result.forEach((e) => {
      total += +e.num;
      switch (e.type) {
        case 1:
          e.text = '首页访问数';
          break;
        case 2:
          e.text = '图标下载数';
          break;
        case 3:
          e.text = '搜索按钮点击数';
          break;

        default:
          break;
      }
    });
    if (result.length > 0) {
      ctx.body = util.resultSuccessJson(undefined, undefined, {
        list: result,
        total: total,
      });
    } else {
      ctx.body = util.resultSuccessJson(undefined, '错误', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
};
